﻿' ****************************************************************************
' Copyright (C) 2009 Tony George
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'
' ****************************************************************************

Public Class frmRun
    Private menuAdded As Boolean = False

    Private Sub frmRun_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If SelectedTask Is Nothing Then
            Me.txtProgram.Text = "%%T\"
            Me.txtArgs.Text = ""
            chkNoWait.Checked = False
            chkHideConsoleWindows.Checked = False
        Else
            Dim s As String = SelectedTask.Text

            'remove escape chars
            s = s.Replace("\\", "\").Replace("\""", """")

            'remove outer quotes
            s = s.Replace("RunProgram=""", "")
            s = s.Remove(s.Length - 1, 1)

            'get prefixes -----------------------

            If s.Contains("nowait:") Then
                s = s.Replace("nowait:", "")
                chkNoWait.Checked = True
            Else
                chkNoWait.Checked = False
            End If

            If s.Contains("hidcon:") Then
                s = s.Replace("hidcon:", "")
                chkHideConsoleWindows.Checked = True
            Else
                chkHideConsoleWindows.Checked = False
            End If

            '-------------------------------------------

            Dim arr() As String = Split(s, """ ")

            If arr(0).Trim <> "" Then
                Me.txtProgram.Text = arr(0).Replace("""", "").Trim
            Else
                Me.txtProgram.Text = ""
            End If

            If arr.Length = 2 Then
                Me.txtArgs.Text = arr(1).Trim
            Else
                Me.txtArgs.Text = ""
            End If
        End If

        If Not menuAdded Then
            addMenuItems(Me.cmVar, Me.txtProgram)
            menuAdded = True
        End If
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        If Me.txtProgram.Text.Trim = "" Or Me.txtProgram.Text.Trim = "%%T\" Then
            MsgBox(GetLangString("2114"))
            Exit Sub
        End If

        Dim s As String = ""

        'append prefixes
        If chkNoWait.Checked Then
            s += "nowait:"
        End If
        If chkHideConsoleWindows.Checked Then
            s += "hidcon:"
        End If

        'append path
        s += """" + Me.txtProgram.Text + """"

        'append args
        If Me.txtArgs.Text.Trim <> "" Then
            s += " " & Me.txtArgs.Text
        End If

        'insert escape chars
        s = s.Replace("\", "\\").Replace("""", "\""")

        'outer quotes must not be escaped
        s = "RunProgram=""" + s + """"

        'examples------------------------------
        'RunProgram="notepad.exe" RunProgram = "notepad.exe"
        'RunProgram="C:\\Windows\\system32\\notepad.exe" RunProgram = "C: \ \ Windows \ \ system32 \ \ notepad.exe"
        'RunProgram="fm0:nowait:7z_RU.chm" RunProgram = "fm0: nowait: 7z_RU.chm"
        'RunProgram="\"%%T\\Reader7Rus.msi\" /qn" RunProgram = "\"%% T \ \ Reader7Rus.msi \ "/ qn"
        'RunProgram="hidcon:fm0:\"%%S\\install.cmd\" /Q" RunProgram = "hidcon: fm0: \"%% S \ \ install.cmd \ "/ Q" 

        If SelectedTask Is Nothing Then
            frmMain.lvTasks.Items.Add(s).Checked = True
        Else
            SelectedTask.Text = s
        End If

        Me.Close()
    End Sub

    Private Sub cmVar_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmVar.Opening
        Dim cm As ContextMenuStrip = DirectCast(sender, ContextMenuStrip)
        ParentTextBox = cm.SourceControl
    End Sub
End Class